package icangyu.base.utils;

import android.text.TextUtils;

import java.util.IllegalFormatException;

/**
 * Created by Blensmile on 2017/5/26.
 */

public class StringUtils {

    /** 包装String的format方法, 捕获异常
     *
     * @param format
     * @param args
     * @return
     */
    public static String format(String format,Object... args){
        try {
            return String.format(format,args);
        }catch (IllegalFormatException|NullPointerException e){
            e.printStackTrace();
            return "";
        }
    }


    /** 把string转换为整型数
     *
     * @param str
     * @return
     */
    public static int getInteger(String str){
       return getInteger(str,0) ;
    }

    public static float getFloat(String str){
        if(TextUtils.isEmpty(str)){
            return 0f;
        }
        try {
            return Float.parseFloat(str);
        }catch (NumberFormatException e){
            return 0;
        }
    }

    public static int getInteger(String str,int defValue){
        if(TextUtils.isEmpty(str)){
            return defValue;
        }
        try {
            return Integer.parseInt(str);
        }catch (NumberFormatException e){
            return defValue;
        }
    }

    public static long getLong(String str){
        return  getLong(str,0);
    }

    public static long getLong(String str,long defValue){
        if(TextUtils.isEmpty(str)){
            return defValue;
        }
        try {
            return Long.parseLong(str);
        }catch (NumberFormatException e){
            return defValue;
        }
    }

    /**
     * 格式化价格,每3位分隔
     *
     * @param price
     * @return
     */
    public static String formatPricePos(int price){
        if(price>0) {
            return format("￥%1$,3d", price);
        }else {
            return "";
        }
    }

    public static String formatPrice(int price){
        if(price >=0){
            return format("￥%1$,3d", price);
        }else {
            return "";
        }
    }

    public static String formatPriceRaw(String price){
        return formatPriceRaw(getInteger(price,0));
    }

    public static String formatPriceRaw(int price){
        return format("￥%1$,3d", price);
    }

    public static String formatPrice(float price){
        return format("￥%1$,3.2f", price);
    }

    public static String formatPrice(String price){
        return  formatPrice(getInteger(price,-1));
    }

    public static String formatPriceComma(String price){
        return formatPriceComma(getInteger(price));
    }

    public static String formatPriceComma(int price){
        return format("%1$,3d",price);
    }

    public static String formatPricePos(String price){
       return formatPricePos( getInteger(price) );
    }

    public static String formatPriceUncheck(String price){
        return formatPriceUncheck( getInteger(price) );
    }

    public static String formatPriceUncheck(int price){
        if(price>=0){
            return format("￥%1$,3d", price);
        }else {
            return format("-￥%1$,3d", -price);
        }
    }

    public static String formatCoupon(int price){
        if(price>=0) {
            return format("- ￥%1$,3d", price);
        }else {
            return "";
        }
    }

    public static String getTime(int time) {
        int day = time/(24*60*60);
        time = time%(24*60*60);
        int hour = time/(60*60);
        StringBuilder builder = new StringBuilder();
        if(day>0){
            builder.append(day)
                    .append("天")
                    .append(hour)
                    .append("小时");
        }else {
            time = time%(60*60);
            int minut = time/60;
            int second = time%60;
            builder.append(hour)
                    .append(":")
                    .append(minut)
                    .append(":")
                    .append(second);
        }

        return builder.toString();
    }

    public static String mergeStr(String[] str, String gap){
        if(str !=null && gap != null) {
            StringBuilder builder = new StringBuilder();
            for (String item : str) {
                builder.append(item).append(gap);
            }
            if (builder.length() >= gap.length()) {
                builder.delete(builder.length()-gap.length(),builder.length());
            }
            return builder.toString();
        }
        return "";
    }
}
